﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

//Class for rendering progressbar. Keep the boolean "reverse" true, otherwise the progressbar will be invisible
namespace TrenchcoatRobots.Classes.ComponentClasses.UI {
	class ProgressBar : Component{
		public float Value { get; set; }
		public int UVmultiplier { get; set; }
		public Texture2D Texture { get; set; }
		public bool Horizontal { get; set; }
		public bool Reverse { get; set; }
		public bool Continuous { get; set; }

		protected void Init (){
			Horizontal = true;
			Reverse = true;
			Value = 1;
			UVmultiplier = 1;
			Continuous = false;
		}

		public ProgressBar( ParentMethod gameObjectMethod, Texture2D texture )
			: base( gameObjectMethod, "ProgressBar", false ) {
			Init();
			this.Texture = texture;
		}

		public override void Draw(ref SpriteBatch spriteBatch, ref TrenchcoatRobots.Datatypes.DrawInfo drawinfo) {
			if (Continuous) {
				if (Horizontal) {
					if(Reverse){
					spriteBatch.Draw(
						Texture,
						new Rectangle(
							(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
							(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
							(int)Math.Floor(Texture.Width * UVmultiplier * Value),
							Texture.Height
							),
						new Rectangle(0, 0, (int)Math.Floor(Texture.Width * Value * UVmultiplier), Texture.Height),
						Color.White,
						MathHelper.ToRadians(Parent().Angle),
						new Vector2(Texture.Width, Texture.Height) * 0.5f,
						SpriteEffects.None,
						0f
						);
					} else {
						spriteBatch.Draw(
						Texture,
						new Rectangle(
							(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
							(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
							(int)Math.Floor(Texture.Width * UVmultiplier * Value),
							Texture.Height
							),
						new Rectangle(0, 0, (int)Math.Floor(Texture.Width * Value * UVmultiplier), Texture.Height),
						Color.White,
						MathHelper.ToRadians(Parent().Angle),
						new Vector2(Texture.Width, Texture.Height) * 0.5f,
						SpriteEffects.FlipVertically,
						0f
						);
					}
				}
				else {
					if (Reverse) {
						spriteBatch.Draw(
							Texture,
							new Rectangle(
								(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
								(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
								Texture.Width,
								(int)Math.Floor(Texture.Height * Value * UVmultiplier)
								),
							new Rectangle(0, 0, Texture.Width, (int)Math.Floor(Texture.Height * Value * UVmultiplier)),
							Color.White,
							MathHelper.ToRadians(Parent().Angle),
							new Vector2(Texture.Width, Texture.Height) * 0.5f,
							SpriteEffects.None,
							0f
							);
						} else {
						spriteBatch.Draw(
							Texture,
							new Rectangle(
								(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
								-(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
								Texture.Width,
								(int)Math.Floor(Texture.Height * Value * UVmultiplier)
								),
							new Rectangle(0, 0, Texture.Width, -(int)Math.Floor(Texture.Height * Value * UVmultiplier)),
							Color.White,
							MathHelper.ToRadians(Parent().Angle),
							new Vector2(Texture.Width, Texture.Height) * 0.5f,
							SpriteEffects.None,
							0f
							);
					}
				}
			}
			else {
				if (Horizontal) {
					if (Reverse) {
						spriteBatch.Draw(
							Texture,
							new Rectangle(
								(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
								(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
								Texture.Width * (int)Math.Floor(UVmultiplier * Value),
								Texture.Height
								),
							new Rectangle(0, 0, Texture.Width * (int)Math.Floor(Value * UVmultiplier), Texture.Height),
							Color.White,
							MathHelper.ToRadians(Parent().Angle),
							new Vector2(Texture.Width, Texture.Height) * 0.5f,
							SpriteEffects.None,
							0f
							);
					}
					else {
						spriteBatch.Draw(
							Texture,
							new Rectangle(
								(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
								(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
								-Texture.Width * (int)Math.Floor(UVmultiplier * Value),
								Texture.Height
								),
							new Rectangle(0, 0, Texture.Width * -(int)Math.Floor(Value * UVmultiplier), Texture.Height),
							Color.White,
							MathHelper.ToRadians(Parent().Angle),
							new Vector2(Texture.Width, Texture.Height) * 0.5f,
							SpriteEffects.None,
							0f
							);
					}
				}
				else {
					spriteBatch.Draw(
						Texture,
						new Rectangle(
							(int)Math.Floor((Parent().Position.X + drawinfo.Camera.Position.X) * drawinfo.Camera.Zoom),
							(int)Math.Floor((Parent().Position.Y + drawinfo.Camera.Position.Y) * drawinfo.Camera.Zoom),
							Texture.Width,
							Texture.Height * (int)Math.Floor(UVmultiplier * Value)
							),
						new Rectangle(0, 0, Texture.Width, Texture.Height * (int)Math.Floor(Value * UVmultiplier)),
						Color.White,
						MathHelper.ToRadians(Parent().Angle),
						new Vector2(Texture.Width, Texture.Height) * 0.5f,
						SpriteEffects.None,
						0f
						);
				}
			}
		}

		public override void Update(ref GameTime gameTime) {
			base.Update(ref gameTime);

			Value -= (float)gameTime.ElapsedGameTime.TotalSeconds * 0.1f;
		}
	}
}
